import {
	serverUrl2
} from "../const"
const hideLoading = ['/public/getPublicOpinionDetailStatisticsList','/public/getPublicOpinionStatisticsList']
export default function request(config) {
	let isHideLoading = false;
	if(hideLoading.indexOf(config.url) > -1) {
		isHideLoading = true;
	}
	console.log(isHideLoading,'isHideLoadingisHideLoading')
	// #ifdef H5
	let baseUrl = "/api"
	// #endif
	// #ifdef APP-PLUS || MP
	let baseUrl = 'https://' +serverUrl2+'/api'
	// #endif

	config.url = baseUrl + config.url;
	console.log(config.url,"config.urlconfig.url")
	config.header = {
		"Authorization": uni.getStorageSync('token'),
		"platform": "APP"
	};

	if (uni.getStorageSync("loginUserInfo")) {
		let permission = JSON.parse(uni.getStorageSync("loginUserInfo")).permission;
		console.log(permission);
		let lastPermission;
		if (permission && permission.length) {
			if (permission.indexOf('common') > -1) {
				lastPermission = 'common';
			}
			if (permission.indexOf('admin') > -1) {
				lastPermission = 'admin';
			}
			if (permission.indexOf('superAdmin') > -1) {
				lastPermission = 'superAdmin';
			}
			config.header.permission = lastPermission;
		};

	}

	config.hasLoading = true
	if (config.hasLoading && !isHideLoading) {
		uni.showLoading({
			title: '加载中...'
		})
		uni.showNavigationBarLoading()
	}
	return new Promise((resolve, reject) => {
		uni.request({
			...config,
			withCredentials: true,
			success: (res) => {
				uni.hideLoading()
				uni.hideNavigationBarLoading()
				const {
					code,
					data,
					errMsg
				} = res.data;
				if (code == 0 || code == 200) {
					resolve(res.data);
				} else if (code == 401) {
					uni.showToast({
						title: '请重新登录',
						icon: 'error'
					})
					// uni.redirectTo({
					// 	url: '/pages/login/login'
					// })
				} else {
					if(isHideLoading) {
						reject(res.data);
						return
					}
					uni.showToast({
						title: errMsg,
						icon: 'error'
					});
				}
			},
			fail: (rej) => {
				uni.hideLoading()
				uni.hideNavigationBarLoading()
				if(isHideLoading) {
					reject(rej)
					return
				}
				uni.showToast({
					title: "服务器出错",
					icon: 'none'
				})
			}
		})
	})
}
